require('./util/TimeHelper');
let hi = require('./util/HttpHelper');
let cheerio = require('cheerio');

let user = {
    name: '登陆账户',
    pwd: '密码自己填写',
    cookie: "Hm_lvt_f9e9e990727d0fa279f142095b569b70=1573179890; _ga=GA1.2.1872693557.1573179891; _gid=GA1.2.239762535.1573179891; fechat_token=PbxE6qmJFtpHo9XRhCVKlmsEdTz1OXfOUcL6qtf0-eAOVTRhVWyvxtpRoxzKDf1IDdeDNOxENJOArZPQqXtaLlyVXAXv37pO7XowcacPnVqJhSnYqnJGZYJd4f04w4WrOcxV5GIqQ6O4BefocYilr9Iuhl2NFzdHjT6Fxqq0P931Oli2Cpu2JTUeFs1Tw2cRK0LFchYztuc2N5jfY2iWSdNTw0ACQyYeMdVQ-rR1L_-PqjfQOdF5Lzhvi-szySP_yZiBFNx91aPEMjhbamDo-bTV7iYwNX77uEwet8pyaV9O8u8sMcmkUHHL66c_1qr-TPY75klBWEEAKQAtm1YNvLNpcS8asf_HkFWmc8hhUZzxTmx5BGopgInHuct9Vrj2oyjklv2JxQ65hT_8TGXsnJAvXYiRTwQDjkjEcWHII0bU4ha5H7uzRRRHKF7yCTRU3Zp6ZIHDYjtjmyNrO_sVaw==; Hm_lpvt_f9e9e990727d0fa279f142095b569b70=1573180667; _gat_gtag_UA_140380407_1=1"
};

//回复方法
function postMessage(id) {
    let info = "帖子ID" + id + "，来自易支烟的NodeJs水贴 https://blog.1zyan.cn " + new Date().format('yyyy-MM-dd hh:mm:ss');
    hi.Ajax({
        url: "https://www.fechat.ink/comments/create",
        method: "POST",
        form: {
            topics_id: id,
            content: info
        },
        headers: {
            Cookie: user.cookie
        }
    }).then((obj) => {
        //水贴成功重定向页面
        if (obj.statusCode === 302) {
            console.log('水贴成功：' + id);
        } else {
            console.error('水贴失败：' + id);
        }
    });
}
//取评论列表
function GetPing(id){
    //获取帖子详情
    hi.Ajax({
        url:"https://www.fechat.ink/topics/"+id
    }).then((res)=>{
        let $ = cheerio.load(res.body);
        let lis=$(".reply_list .list .user");
        let list=[];
        for (let i=0;i<lis.length;i++){
            //是否为当前用户名
            if($(lis[i]).find(".author a").attr('title')=="yizhiyan"){
                //取评论id
                let id=$(lis[i]).find(".power a:first-of-type").attr("href").match(/(?<=\/)([\d]+)/)[0];
                //修改评论
            }
        }
    });
}
//取帖子列表
function GetIds(){
    //获取帖子列表
    for (let page=1;page<=3;page++){
        hi.Ajax({
            url:"https://www.fechat.ink/?nodeId=&tags=&pages="+page
        }).then((res)=>{
            let $ = cheerio.load(res.body);
            let lis=$(".list_item .info a");
            for (let i=0;i<lis.length;i++){
                //获取帖子ID
                let id=$(lis[i]).attr('href').match(/(?<=\/)([\d]+)/)[0];
                //水贴
                GetPing(id);
            }
        });
    }
}
hi.Ajax({
    url: "https://www.fechat.ink/sign/signIn",
    form: {
        email: user.name,
        password: user.pwd
    },
    headers: {
        "user-agent": `Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36`,
        "content-type": "application/x-www-form-urlencoded",
        "origin": "https://www.fechat.ink",
        "referer": "https://www.fechat.ink/login"
    },
    method: "POST"
}).then((obj) => {
    if (obj.body.includes('登录FE')) {
        //解析html得到信息
        let $ = cheerio.load(obj.body);
        console.log($(".uk-alert-warning p").text());
    } else if (obj.statusCode === 302) {
        //保存cookie
        user.cookie = obj.headers['set-cookie'];
        console.log('登陆成功');
        return obj;
    } else {
        console.log(obj.body);
    }
}).then(() => {
    //开始水贴
    GetIds();
});
